package com.baizhi.dao;

import com.baizhi.entity.User;
import com.baizhi.util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDAOImpl implements UserDAO {

    @Override
    public User queryByUserAndPassword(String account, String password) {
        Connection conn = null;
        Statement stmt = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = "select userid,name,account,password,fansnumber from user where account=? and password=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, account);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            User user = null;
            if (rs.next()) {
                user = new User();
                user.setUserid(rs.getInt(1));
                user.setName(rs.getString(2));
                user.setAccount(rs.getString(3));
                user.setPassword(rs.getString(4));
                user.setFansnumber(rs.getInt(5));
            }
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                rs = null;
                if (stmt != null) {
                    stmt.close();
                }
                stmt = null;
                if (pstmt != null) {
                    pstmt.close();
                }
                pstmt = null;
                if (conn != null) {
                    conn.close();
                }
                conn = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void register(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = "insert into user(userid,name,account,password,sex,question,answer,introduce,fansnumber) values (?,?,?,?,?,?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, user.getUserid());
            pstmt.setString(2, user.getName());
            pstmt.setString(3, user.getAccount());
            pstmt.setString(4, user.getPassword());
            pstmt.setString(5, user.getSex());
            pstmt.setString(6, user.getQuestion());
            pstmt.setString(7, user.getAnswer());
            pstmt.setString(8, user.getIntroduce());
            pstmt.setInt(9, user.getFansnumber());
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                pstmt = null;
                if (conn != null) {
                    conn.close();
                }
                conn = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public List<User> queryAll() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = "select userid,name,account,password,sex,question,answer,introduce,fansnumber from user";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            List<User> list = new ArrayList<>();
            while (rs.next()) {
                User u = new User();
                u.setUserid(rs.getInt(1));
                u.setName(rs.getString(2));
                u.setAccount(rs.getString(3));
                u.setPassword(rs.getString(4));
                u.setSex(rs.getString(5));
                u.setQuestion(rs.getString(6));
                u.setAnswer(rs.getString(7));
                u.setIntroduce(rs.getString(8));
                u.setFansnumber(rs.getInt(9));
                list.add(u);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                pstmt = null;
                if (conn != null) {
                    conn.close();
                }
                conn = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public User ShowFansNumberAndIntroduce(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = "select userid,name,account,password,sex,fansnumber,question,answer,introduce from user where account like ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,user.getAccount());
            rs = pstmt.executeQuery();
            User u = null;
            if(rs.next()){
                u = new User();
                u.setUserid(rs.getInt(1));
                u.setName(rs.getString(2));
                u.setAccount(rs.getString(3));
                u.setPassword(rs.getString(4));
                u.setSex(rs.getString(5));
                u.setFansnumber(rs.getInt(6));
                u.setQuestion(rs.getString(7));
                u.setAnswer(rs.getString(8));
                u.setIntroduce(rs.getString(9));
            }
            return u;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                pstmt = null;
                if (conn != null) {
                    conn.close();
                }
                conn = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}